package edu.bit.water.service;


import edu.bit.water.dao.LabelCleanProduceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import java.util.*;

import edu.bit.water.entity.LabelCleanProduce;

/**
 * meta_assessment_indicator数据表对应的service类
 * @Author: Page
 */
@Service
public class LabelCleanProduceService {

	@Autowired
	private LabelCleanProduceDao LabelCleanProduceDao;

	@Autowired
	private EntityEffluentLetService entityEffluentLetService;

	public List<LabelCleanProduce> selectLabelCleanProduce() {
		return LabelCleanProduceDao.selectLabelCleanProduce();
	}

	public Map<String, Object> insertLabelCleanProduce(LabelCleanProduce LabelCleanProduce) {
		Map<String, Object> result = new HashMap<>();
		Integer id = LabelCleanProduceDao.getMaxId();
		if (id != null) {
			LabelCleanProduce.setId(id + 1);
		} else {
			LabelCleanProduce.setId(0);
		}
		int tmp = LabelCleanProduceDao.insertLabelCleanProduce(LabelCleanProduce);
		if (tmp == 1) {
			result.put("status", "success");
		}
		else {
			result.put("status", "failed");
		}
		return result;
	}

	public Map<String, Object> deleteLabelCleanProduce(List<String> ids) {
		Map<String, Object> result = new HashMap<>();
		boolean flag = true;
		for (int i = 0; i < ids.size(); i++) {
			int temp = entityEffluentLetService.deleteByWasteWaterId(Integer.parseInt(ids.get(i)));
			//System.out.println(temp);
			int tmp = LabelCleanProduceDao.deleteLabelCleanProduce(Integer.parseInt(ids.get(i)));
			//System.out.println(tmp);
			if (tmp > 0 && temp >= 0) {
				result.put(ids.get(i).toString(), "success");
			}
			else {
				result.put(ids.get(i).toString(), "failed");
				flag = false;
			}
		}
		if (flag) {
			result.put("status", "success");
		}
		else {
			result.put("status", "failed");
		}
		return result;
	}

	public Map<String, Object> updateLabelCleanProduce(LabelCleanProduce LabelCleanProduce) {
		Map<String, Object> result = new HashMap<>();
		int tmp = LabelCleanProduceDao.updateLabelCleanProduce(LabelCleanProduce);
		if(tmp == 1) {
			result.put("status", "success");
		}
		else {
			result.put("status", "failed");
		}
		return result;
	}
}